//
//  MahuaCell.m
//  KL
//
//  Created by mac on 15/12/14.
//  Copyright © 2015年 zhouxubin. All rights reserved.
//
#define KLIconBaseUrl @"http://pic.qiushibaike.com/system/avtnew/"
#define KLPictureUrl @"http://pic.qiushibaike.com/system/pictures/"
#define KLContentFont(x) [UIFont systemFontOfSize:x]
#define KLNameLabelFont(x) [UIFont systemFontOfSize:x]

#import "MahuaCell.h"
#import "Mahua.h"
#import <UIImageView+WebCache.h>
#import <SVProgressHUD.h>

@interface MahuaCell () <UIWebViewDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *pictureHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *picutureTop;

@property (weak, nonatomic) IBOutlet UIButton *likeButton;
@property (weak, nonatomic) IBOutlet UIButton *unlikeButton;
@property (weak, nonatomic) IBOutlet UIButton *shareButton;

@end

@implementation MahuaCell

#pragma mark - 按钮的点击事件
// 喜欢按钮的点击事件
- (IBAction)likeButton:(UIButton *)sender {
    sender.selected = !sender.selected;
    // 判断不喜欢的有没有被选中,如果是选中的话那么改为没有选中
    if (self.unlikeButton.selected) {
        self.unlikeButton.selected = NO;
    }
    // 把likelabel显示黑色
    if (sender.selected) {
//        self.likeCountLabel.textColor = [UIColor redColor];
//        self.likeCountLabel.text = [NSString stringWithFormat:@"喜欢 %d", self.mahua.votes.up - self.mahua.votes.down + 1];
    }
}

// 不喜欢的按钮的点击事件
- (IBAction)unlikeButton:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (self.likeButton.selected) {
        self.likeButton.selected = NO;
//        self.likeCountLabel.textColor = [UIColor lightGrayColor];
    }
    if (sender.selected) {
//        self.likeCountLabel.text = [NSString stringWithFormat:@"喜欢 %d", self.mahua.votes.up - self.mahua.votes.down - 1];
        // 弹出提示框
        if ([self.delegate respondsToSelector:@selector(mahuaCellDidClickUnlikeButton:)]) {
            [self.delegate mahuaCellDidClickUnlikeButton:self];
        }
    }
}

// 分享按钮的点击事件
- (IBAction)shareButton:(UIButton *)sender {
    if ([self.delegate respondsToSelector:@selector(mahuaCellDidClickShareButton:)]) {
        [self.delegate mahuaCellDidClickShareButton:self];
    }
}

#pragma mark - 重写setter方法
- (void)setMahua:(Mahua *)mahua {
    _mahua = mahua;
    User *user = mahua.user;
    
    // 头像 http://pic.qiushibaike.com/system/avtnew/ + (user的id属性的前四位) + 整个的id + medium + 图片的url  http://pic.qiushibaike.com/system/avtnew/1306/13065235/medium/20151202142918.jpg
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@/%@/medium/%@", KLIconBaseUrl,user.prefixID,user.ID ,user.icon]] placeholderImage:[UIImage imageNamed:@"userPlaceholder"] options:SDWebImageProgressiveDownload];
    
    // 昵称
    if (user.login.length) {
        self.nameLabel.text = user.login;
    }else {
        self.nameLabel.text = @"匿名用户😘";
    }
    // 内容
    self.contentLabel.text = mahua.content;
    
    if (mahua.image.length) {
        CGFloat scale = KLScreenWidth / [mahua.image_size.s[1] floatValue];
        // 设置配图
        // http://pic.qiushibaike.com/system/pictures/ + (11424)这个是id得前5位数字 + 整个的id + medium + 图片的url
        self.pictureHeight.constant = [mahua.image_size.s[1] floatValue] * scale;
        [self.pictureView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@/%@/medium/%@", KLPictureUrl, mahua.prefixID, mahua.ID, mahua.image]] placeholderImage:[UIImage imageNamed:@"placeholder"] options:SDWebImageProgressiveDownload | SDWebImageDelayPlaceholder];
    }
}

- (void)awakeFromNib {
    // 设置cell的点击样式
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    // 设置头像的图层
    self.iconView.layer.cornerRadius = 22.5;
    self.iconView.layer.masksToBounds = YES;
    // 设置昵称的颜色
    self.nameLabel.textColor = [UIColor darkGrayColor];
    self.nameLabel.font = KLNameLabelFont(15);
}

#pragma mark - webViewDelegate

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
